Pine脚本的新功能:str.format() 和 array.from() | 您所在的位置:网站首页 › tradingview pine转python › Pine脚本的新功能:str.format() 和 array.from() |
Pine脚本语言的这项更新添加了用于处理字符串和阵列的函数。 str.format() 函数允许您将传递给它的参数转换为带有指定格式的字符串: str.format(formatString, arg0, arg1, ...) -> stringformatString 在括号 “{}” 中包含一个 N 下标以替换相应的格式化参数(arg0, arg1..argN,它们是字符串或数字),以及下面讨论的一些形态。 因此,对于解析字符串,您不再需要运用使用 str.replace_all() 函数的结构: txt := "Time to {0} some {1}!" txt := str.replace_all(txt, "{0}", close > open ? "buy" : "sell") txt := str.replace_all(txt, "{1}", syminfo.ticker)但是你可以用一种新的、更方便、更短的方式来做到这一点: txt = str.format("Time to {0} some {1}!", close > open ? "buy" : "sell", syminfo.ticker)括号中的下标 {0} 和 {1} 替换为相应的序数参数:{0} – 三元运算符执行的结果,字符串 “buy” 或 “sell” ,根据收盘价和开盘价的不等式返回,{1} – syminfo.ticker,图表上的当前交易品种: 使用 number 模板,可以将转换为字符串的数字转换为不同的表示形式,例如限制小数位数: str.format("{0,number,#.#}", 1.34) // returns: 1.3去掉数字的小数部分: str.format("{0,number,integer}", 1.34) // returns: 1获取财务信息: str.format("The cash turnover amounted to {0,number,currency}", 1340000) // “The cash turnover amounted to $1,340,000.00”.获取百分比: str.format("Expected return is {0,number,percent} - {1,number,percent}", 0.1, 0.2) // returns: Expected return is 10% - 20%.str.format() 函数支持用于格式化日期/时间的 date 模板。 此脚本显示自年初以来的月数、周数和天数: //@version=4 study("str_format_date") //formatting string with date pattern txt = str.format("Current bar date: {0,date, Y-MM-d'.' \n M 'months \n' F 'weeks \n' D 'days \n from the beginning of the year' }", timenow) //printing label var l1 = label.new(na, na, color = #FFFFFF00) label.set_x(l1, bar_index) label.set_yloc(l1, yloc.belowbar) label.set_text(l1, txt) plot(close)只有一个变量 timenow 被传递到 str.format() 函数,该函数包含 UNIX 时间表现形式:图表上交易品种的当前日期、时间和时区。 该字符串使用字符Y, M, D, F(年、月、日、本月的一周)进行格式化,\n 特殊字符用于换行,单引号 ‘ ’ 用于在字符之间插入文本。 此脚本将格式化的字符串输出到标签,并使用各种字符输出 str.format() 函数的相应结果: //@version=4 study("str_format_date_time") // creating date-time variable: 2021.05.04. 14:30:59 t = timestamp("GMT+0", 2021, 05, 04, 14, 30, 59) // function for left side of the label f_print_left(_text) => var _label = label.new(bar_index, na, _text, xloc.bar_index, yloc.price, color(na), label.style_label_right, color.green, size.large, text.align_right), label.set_xy(_label, bar_index, 0), label.set_text(_label, _text) // function for right side of the label f_print_right(_text) => var _label = label.new(bar_index, na, _text, xloc.bar_index, yloc.price, color(na), label.style_label_left, color.red, size.large, text.align_left), label.set_xy(_label, bar_index, 0), label.set_text(_label, _text) var string format = "{0,date,y.MM.dd hh:mm:ss}\n" + "{1,date,short}\n" + "{2,date,medium}\n" + "{3,date,long}\n" + "{4,date,full}\n" + "{5,date,h a z (zzzz)}\n" + "{6,time,short}\n" + "{7,time,medium}\n" + "{8,time,long}\n" + "{9,time,full}\n" + "{10,time,hh:mm:ss}\n" + "{11,time,HH:mm:ss}\n" // printing format string f_print_left(format) //printing formatted string f_print_right(str.format(format, t, t, t, t, t, t, t, t, t, t, t, t))标签的左列包含 {} 括起来的字符,右列是 str.format 函数的结果: t 变量传递给 str.format() 函数,该函数包含 UNIX 时间表现形式:当前日期、时间和时区。 下一个创新是 array.from() 函数,它采用以下类型之一的可变数量的输入参数:float, int, bool, string, label, line, color,并返回相应类型的阵列。该函数允许您在一行代码中声明一个阵列,为其分配初始值并将结果分配给任何变量。 以前,要创建一个阵列并用初始值填充它,您必须使用 array.new() 函数和 array.push() 或 array.set() 函数: color[] plotColors = array.new_color(size=0) array.push(plotColors, color.red) array.push(plotColors, color.green) array.push(plotColors, #0000FF)现在,要创建一个包含图表颜色的阵列,您只需一行即可: plotColors = array.from(color.red, color.green, #0000FF)该函数将根据接收到的参数自动将 plotColors 阵列转换为 color[] 类型: //@version=4 study(title="array_from") f_arr (series1, series2, color1, color2) => [ array.from(series1, series2), array.from(color1, color2) ] [lines, colors] = f_arr(close, open, color.red, color.green) plot ( array.get(lines, 0), color = array.get(colors, 0)) plot ( array.get(lines, 1), color = array.get(colors, 1))我们的参考手册中始终提供有关这些 str.format() 和 array.from() 函数的信息。 如果您想了解 Pine 脚本更新,请查看发行说明部分。 PineCoders 帐户还在其 Squawk Box Telegram 频道、Twitter 和 TradingView 上的Pine脚本公开聊天室中公布更新。 我们希望大家发现这些改进有用,并请继续与我们分享您的意见。我们正在为用户打造 TradingView,很高兴听到您对我们创新的看法。
还没有关注我们的中文微信公众号?快来扫二维码吧! |
CopyRight 2018-2019 实验室设备网 版权所有 |